package pro.softzhang.algo.lc1300;

import pro.softzhang.algo.flag.difficulty.DRed;
import pro.softzhang.algo.flag.knowledge.KDynamicPlanning;

import java.util.Arrays;

/**
 *
 */
@DRed
public class LC1335_MinimumDifficultyOfAJobSchedule {
    public static void main(String[] args) {

    }

    /**
     *
     */
    @KDynamicPlanning
    static
    class Solution {
        public int minDifficulty(int[] jobDifficulty, int d) {
            int n = jobDifficulty.length;
            if (n < d) return -1;


            int[][] dp = new int[n + 1][d + 1];
            int inf = 1 << 30;
            for (int[] arr : dp) {
                Arrays.fill(arr, inf);
            }

            dp[0][0] = 0;
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= Math.min(d, i); ++j) {
                    int max = 0;
                    for (int k = i; k > 0; --k) {
                        max = Math.max(max, jobDifficulty[k - 1]);
                        dp[i][j] = Math.min(dp[i][j], dp[k - 1][j - 1] + max);
                    }
                }
            }
            return dp[n][d] >= inf ? -1 : dp[n][d];
        }
    }
}
